package com.sheledon.flyCat.core.aop.factory;

import com.sheledon.flyCat.annotation.aop.Aspect;
import com.sheledon.flyCat.common.utils.ReflectionUtils;
import com.sheledon.flyCat.core.aop.intercept.AspectInterceptor;
import com.sheledon.flyCat.core.aop.intercept.Interceptor;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author sheledon
 */
public class InterceptFactory {
    private static final List<Interceptor> interceptors = new ArrayList<>();
    public static void loadArrInterceptors(String [] packagesName){
        Set<Class<? extends Interceptor>> classes = ReflectionUtils.scanSubClass(packagesName, Interceptor.class);
        Set<Class<?>> aspectClasses = ReflectionUtils.scanAnnotationClass(packagesName, Aspect.class);
        classes.forEach(c->{
            interceptors.add((Interceptor) ReflectionUtils.newInstance(c));
        });
        aspectClasses.forEach(ac->{
            Object instance = ReflectionUtils.newInstance(ac);
            interceptors.add(new AspectInterceptor(instance));
        });
    }
    public static List<Interceptor> getInterceptors() {
        return interceptors;
    }
}
